package com.bawei.demo_shoppingcar.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bawei.demo_shoppingcar.R;
import com.bawei.demo_shoppingcar.activity.CommodityActivity;
import com.bawei.demo_shoppingcar.activity.TabLayoutActivity;
import com.bawei.demo_shoppingcar.adapter.ClassifyRecyclerAdapter;
import com.bawei.demo_shoppingcar.application.ContentData;
import com.bawei.demo_shoppingcar.bean.ClassifyBean;
import com.bawei.demo_shoppingcar.bean.CommdityBean;
import com.google.gson.Gson;

import java.util.List;


public class FenLeiFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private RecyclerView recycler;
    private ClassifyRecyclerAdapter adapter;
    private ClassifyBean classifyBean;
    private SwipeRefreshLayout fl_swipeRefereshLayout;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            fl_swipeRefereshLayout.setRefreshing(true);
            initData();
        }
    };
    private ClassifyBean.DataBean data;

    public FenLeiFragment() {
    }


    public static FenLeiFragment newInstance(String param1, String param2) {
        FenLeiFragment fragment = new FenLeiFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_fen_lei, container, false);
        initView(inflate);

        initData();

        return inflate;
    }

    private void initRecyclerView() {
        data = classifyBean.getData();
        adapter = new ClassifyRecyclerAdapter(getContext(), data);
        recycler.setAdapter(adapter);
        //TabLayout 监听
        adapter.setTabLayoutClickListener(new ClassifyRecyclerAdapter.MyClassTabLayoutClickListener() {
            @Override
            public void onItemClick(View view, int position) {

                startActivity(new Intent(getActivity(), TabLayoutActivity.class));
            }
        });



        //列表监听
        adapter.setDefaultItemClickListener(new ClassifyRecyclerAdapter.MyClassDefaultItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                List<ClassifyBean.DataBean.GoodsBriefBean> goodsBrief = data.getGoodsBrief();
                ClassifyBean.DataBean.GoodsBriefBean goodsBriefBean = goodsBrief.get(position - 2);
                String goods_img = goodsBriefBean.getGoods_img();
                String goods_name = goodsBriefBean.getGoods_name();
                double shop_price = goodsBriefBean.getShop_price();
                CommdityBean commdityBean = new CommdityBean(goods_img, goods_name, shop_price);
                Bundle bundle = new Bundle();
                bundle.putSerializable("data", commdityBean);
                Intent intent = new Intent(getActivity(), CommodityActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    private void initData() {

        RequestQueue requestQueue = Volley.newRequestQueue(getContext());
        //定义一个请求
        StringRequest stringRequest = new StringRequest(Request.Method.GET, ContentData.classifyURL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                //解析
                classifyBean = gson.fromJson(response, ClassifyBean.class);

                initRecyclerView();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        //进行请求
        requestQueue.add(stringRequest);

    }

    private void initView(View inflate) {

        recycler = (RecyclerView) inflate.findViewById(R.id.recycler_fenlei);
        fl_swipeRefereshLayout = (SwipeRefreshLayout) inflate.findViewById(R.id.fl_swipeRefereshLayout);
        fl_swipeRefereshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                fl_swipeRefereshLayout.setRefreshing(false);
                mHandler.sendEmptyMessageDelayed(1, 2000);
            }
        });

        GridLayoutManager manager = new GridLayoutManager(getContext(), 2);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position > 1) {
                    return 1;
                }
                return 2;
            }
        });
        recycler.setLayoutManager(manager);
    }

}
